package com.shuyuntu.proposal.controller;

import com.shuyuntu.proposal.entity.Attachment;
import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.entity.Department;
import com.shuyuntu.proposal.entity.Proposal;
import com.shuyuntu.proposal.enums.EProposalState;
import com.shuyuntu.proposal.enums.EProposalType;
import com.shuyuntu.proposal.security.UseSecurity;
import com.shuyuntu.proposal.service.*;
import com.shuyuntu.proposal.vo.PostedProposalVO;
import org.hibernate.exception.SQLGrammarException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by limeng on 16-4-25.
 */
@Controller
@RequestMapping("/proposal")
public class ProposalController {
    /**
     * 将提案保存到服务器
     * @param postedProposal post到服务器的提案信息
     * @param session       注入的session实例
     * @return  上传成功与否的json对象
     */
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public Map<String, Object> postNewProposal(
        @RequestBody PostedProposalVO postedProposal, HttpSession session) {
        Map<String, Object> resultMap = new LinkedHashMap<>();

        //字段合法性检查
        if (postedProposal.getTitle() == null || postedProposal.getTitle().equals("")) {
            resultMap.put("state", false);
            resultMap.put("result", "标题不允许为空");
            return resultMap;
        }
        //检查是不是当前用户写的
        if (session.getAttribute("loginUserId").equals(postedProposal.getAuthorId())) {
            Proposal proposal = new Proposal();

            proposal.setTitle(postedProposal.getTitle());
            proposal.setContent(postedProposal.getContent());
            try {
                proposal.setAuthor(delegateService.getDelegateById(postedProposal.getAuthorId()));
            } catch (Exception e) {
                e.printStackTrace();
                resultMap.put("state", false);
                resultMap.put("result", "监测到非法数据");

                return resultMap;
            }

            Date date = new Date();
            proposal.setCreateTime(date);
            proposal.setUpdateTime(date);

            if (postedProposal.getState() == EProposalState.DRAFT) {
                proposal.setProposalState(EProposalState.DRAFT);
            } else {
                proposal.setProposalState(EProposalState.UNCHECKED);
            }
            proposal.setProposalType(postedProposal.getType());
            proposal.setClickCount(0);

            if (postedProposal.getHostDepartmentId() != null &&
                    !postedProposal.getHostDepartmentId().equals("")) {
                Department hostDepartment =
                        departmentService.getDepartmentById(postedProposal.getHostDepartmentId());
                if (hostDepartment != null) {
                    proposal.setHostDepartment(hostDepartment);
                }
            }

            if (postedProposal.getAssistDepartmentIdList() != null &&
                    postedProposal.getAssistDepartmentIdList().size() > 0) {
                for (String departmentId : postedProposal.getAssistDepartmentIdList()) {
                    Department assistDepartment =
                            departmentService.getDepartmentById(departmentId);
                    if (departmentId != null) {
                        proposal.getAssistDepartments().add(assistDepartment);
                    }
                }
            }
            proposal.getAttachments().addAll(postedProposal.getAttachments());
            //插入数据
            if (proposalService.publishProposal(proposal)) {
                resultMap.put("state", true);
            } else {
                resultMap.put("state", false);
                resultMap.put("result", "数据库出现异常,请联系维护人员");
            }
        } else {
            resultMap.put("state", false);
            resultMap.put("result", "您的身份验证失败！");
        }

        return resultMap;
    }

    /**
     * 更新一个提案
     * @param reqMap 请求jsonMap
     * @param session injection
     * @param response injection
     * @return jsonMap
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> updateProposal(@RequestBody Map<String, Object> reqMap,
                                              HttpSession session,
                                              HttpServletResponse response) {
        Integer proposalId = Integer.parseInt((String)reqMap.get("id"));
        Map<String, Object> resMap = new LinkedHashMap<>();

        Proposal proposal = proposalService.getProposalById(proposalId);
        if (proposal == null) {
            resMap.put("state", false);
            resMap.put("result", "找不到指定的提案");
            return resMap;
        }

        String authorId = (String)session.getAttribute("loginUserId");
        if (authorId != null && authorId.equals(proposal.getAuthor().getId())) {
            String title = (String) reqMap.get("title");
            String content = (String) reqMap.get("content");

            EProposalType proposalType = null;
            EProposalState proposalState = null;

            try {
                Integer state = (Integer)reqMap.get("state");
                if (state != null) {
                    if (state != 0 && state != 1) {
                        state = 0;
                    }
                    proposalState = EProposalState.values()[state];
                }
            } catch (ClassCastException e) {
                String stateStr = (String)reqMap.get("state");
                if (stateStr != null) {
                    Integer state = Integer.parseInt(stateStr);
                    if (state != 0 && state != 1) {
                        state = 0;
                    }
                    proposalState = EProposalState.values()[state];
                }
            }

            try {
                Integer type = (Integer)reqMap.get("type");
                if (type != null)
                    proposalType = EProposalType.values()[type];
            } catch (ClassCastException e) {
                String typeStr = (String)reqMap.get("type");
                if (typeStr != null) {
                    Integer type = Integer.parseInt(typeStr);
                    proposalType = EProposalType.values()[type];
                }
            }

            ArrayList<Map<String, Object>> attachments =
                    (ArrayList<Map<String, Object>>) reqMap.get("attachments");
            if (attachments != null) {
                List<Attachment> attachmentList = new LinkedList<>();

                for (Map<String, Object> attachmentMap : attachments) {
                    Integer attachmentId = null;
                    try {
                        attachmentId = (Integer)attachmentMap.get("id");
                    } catch (ClassCastException e) {
                        String idStr = (String) attachmentMap.get("id");
                        if (idStr != null) {
                            attachmentId = Integer.parseInt(idStr);
                        }
                    }

                    if (attachmentId != null) {
                        Attachment attachment = attachmentService.getById(attachmentId);
                        attachmentList.add(attachment);
                    } else {
                        Attachment attachment = new Attachment();
                        String fileName = (String) attachmentMap.get("filename");
                        String realName = (String) attachmentMap.get("realName");
                        float size = ((Double) attachmentMap.get("size")).floatValue();
                        attachment.setFilename(fileName);
                        attachment.setRealName(realName);
                        attachment.setSize(size);
                        attachment.setProposal(proposal);
                        attachmentService.insertAttachment(attachment);
                        attachmentList.add(attachment);
                    }
                }

                proposal.getAttachments().clear();
                proposal.getAttachments().addAll(attachmentList);
            }

            if (title != null && !title.equals("")) {
                proposal.setTitle(title);
            }
            proposal.setContent(content);
            proposal.setUpdateTime(new Date());
            proposal.setProposalType(proposalType);
            if (proposalState != null)
                proposal.setProposalState(proposalState);
            if (proposalService.updateProposal(proposal)) {
                resMap.put("state", true);
            } else {
                resMap.put("state", false);
                resMap.put("result", "数据库异常!");
            }
            return resMap;
        }

        response.setStatus(404);
        resMap.put("state", false);
        resMap.put("result", "权限不足!");
        return resMap;
    }

    /**
     * 删除一个提案
     * @param proposalId 待删除提案的id
     * @param session injection
     * @return state json
     */
    @ResponseBody
    @RequestMapping(value = "/{proposalId}", method = RequestMethod.DELETE)
    public Map<String, Object> deleteProposal(
            @PathVariable int proposalId,
            HttpSession session) {
        Proposal proposal = proposalService.getProposalById(proposalId);
        String authorId = (String)session.getAttribute("loginUserId");
        Map<String, Object> resMap = new LinkedHashMap<>();
        if (authorId != null && authorId.equals(proposal.getAuthor().getId())) {
            if (proposalService.deleteProposal(proposal)) {
                resMap.put("state", true);
            } else {
                resMap.put("state", false);
                resMap.put("result", "数据库异常!");
            }
            return resMap;
        }
        resMap.put("state", false);
        resMap.put("result", "权限认证失败!");
        return resMap;
    }

    /**
     * 根据指定的条件,返回查询得到的提案列表
     * @param reqMap 请求集合
     *        pageSize : 分页大小
     *        pageNo : 分页页数
     *        authorId : 作者id
     *        proposalState : 提案状态
     *        proposalType : 提案类型
     * @return
     *        state:true/false,
     *        list : [提案列表],
     *        pageCount : 总页数
     */
    @ResponseBody
    @RequestMapping("/search")
    public Map<String ,Object> searchProposal(
            @RequestBody Map<String, Object> reqMap) {
        Integer pageSize = (Integer)reqMap.get("pageSize");
        Integer pageNo = (Integer)reqMap.get("pageNo");

        Map<String, Object> resMap = new LinkedHashMap<>();
        StringBuffer rowCount = new StringBuffer();
        try {
            List<Proposal> list = proposalService.doSearch(pageSize, pageNo, reqMap, rowCount);

            resMap.put("state", true);
            resMap.put("list", list);
            Integer pageCount = (Integer.parseInt(rowCount.toString()) - 1) / pageSize + 1;
            resMap.put("pageCount", pageCount);

        } catch (SQLGrammarException e) {
            e.printStackTrace();
            resMap.put("state", true);
            resMap.put("list", new LinkedList<>());
            resMap.put("pageCount", 1);
        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("state", false);
            resMap.put("result", "数据库发生异常!");
        }

        return resMap;
    }

    /**
     * 专门用来搜索辅助部门提案的接口
     *
     * 辅助部门所属提案的查询十分繁琐,且和普通提案
     * 查询的接口不兼容,所以单开一个方法
     *
     * 要想使用普通的提案查询,请查看<code>searchProposal方法</code>
     *
     * @param reqMap    请求键值对
     * @return 搜索到的提案集合
     */
    @ResponseBody
    @RequestMapping("/searchAssist")
    @UseSecurity(category = "0700")
    public Map<String ,Object> searchAssist(
            @RequestBody Map<String, Object> reqMap, HttpSession session) {
        Integer pageSize = (Integer)reqMap.get("pageSize");
        Integer pageNo = (Integer)reqMap.get("pageNo");
        String assistDepartmentId = (String)reqMap.get("assistDepartmentId");

        Map<String, Object> resMap = new LinkedHashMap<>();
        StringBuffer rowCount = new StringBuffer();
        try {
            List<Proposal> list = proposalService.searchAssist(pageSize, pageNo, assistDepartmentId, rowCount);

            resMap.put("state", true);
            resMap.put("list", list);

            Integer pageCount = (Integer.parseInt(rowCount.toString()) - 1) / pageSize + 1;
            resMap.put("pageCount", pageCount);

        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("state", false);
            resMap.put("result", "数据库发生异常!");
        }

        return resMap;
    }

    /**
     * 获取指定用户的各种提案的数目
     *
     * @param userId 用户id
     * @return 提案数目集合
     */
    @ResponseBody
    @RequestMapping(value = "/count/{userId}", method = RequestMethod.GET)
    @UseSecurity(category = "01")
    public Map<String, Object> getCountInfo(@PathVariable String userId, HttpSession session) {
        Map<String, Object> resultMap = new LinkedHashMap<>();
        try {
            long draftCount = proposalService.getProposalCount(userId, EProposalState.DRAFT);
            long uncheckedCount = proposalService.getProposalCount(userId, EProposalState.UNCHECKED);
            long waitSecondMotionCount =
                    proposalService.getProposalCount(userId, EProposalState.WAIT_SECOND_MOTION);
            long puttedIntoEffectCount = proposalService.getProposalCount(userId, EProposalState.PUTTED_INTO_EFFECT);
            long invalidCount = proposalService.getProposalCount(userId, EProposalState.INVALID);

            Map<String, Long> countMap = new LinkedHashMap<>();
            countMap.put("draft", draftCount);
            countMap.put("unchecked", uncheckedCount);
            countMap.put("waitSecondMotion", waitSecondMotionCount);
            countMap.put("puttedIntoEffect", puttedIntoEffectCount);
            countMap.put("invalid", invalidCount);

            resultMap.put("status", true);
            resultMap.put("result", countMap);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("status", false);
        }

        return resultMap;
    }

    /**
     * 根据提案的id,获取指定的提案,返回json对象
     * @param proposalId 提案的id
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/{proposalId}", method = RequestMethod.GET)
    public Map<String, Object> getProposalById(@PathVariable Integer proposalId) {
        //TODO:权限管理
        Map<String, Object> resultMap = new LinkedHashMap<>();
        try {
            Proposal proposal = proposalService.getProposalAndIncreaseClickCountById(proposalId);
            resultMap.put("state", true);
            resultMap.put("proposal", proposal);
        } catch (Exception e) {
            resultMap.put("state", false);
            resultMap.put("result", "数据库错误!");
        }
        return resultMap;
    }

    /**
     * 检查初审的权限
     * @param checker 审核人
     * @param proposal 被审核的提案
     * @return true/false
     */
    private boolean checkProposalDepartmentCheck(Delegate checker, Proposal proposal) {
        if (checker == null || proposal == null) {
            return false;
        }
        if (checker.getDepartment().getId().equals(proposal.getAuthor().getDepartment().getId())) {
            return true;
        }
        return false;
    }

    /**
     * 将某个提案通过审核
     * @param proposalId 提案的id
     * @return {"status" : true/false, "result" : 错误的原因}
     */
    @RequestMapping(value = "/checkPass/{proposalId}", method = RequestMethod.POST)
    @ResponseBody
    @UseSecurity(category = "02")
    public Map<String, Object> checkPass(@PathVariable int proposalId,
                                         HttpSession session) {
        String userId = (String)session.getAttribute("loginUserId");
        Delegate checker = delegateService.getDelegateById(userId);
        Proposal proposal = proposalService.getProposalById(proposalId);

        Map<String, Object> resultMap = new LinkedHashMap<>();

        if (checkProposalDepartmentCheck(checker, proposal)) {
            int result = proposalService.checkPass(proposalId);
            if (result == 0) {
                resultMap.put("status", true);
                messageService.sendMessageToUser(
                        proposal.getAuthor().getId(),
                        "恭喜！您的提案《"+proposal.getTitle()+"》已通过初审！", true);
            } else if (result == 1) {
                resultMap.put("status", false);
                resultMap.put("result", "访问数据库时遇到错误！");
            } else {
                resultMap.put("status", false);
                resultMap.put("result", "该提案此时不能执行该动作！");
            }
        } else {
            resultMap.put("state", false);
            resultMap.put("result", "权限问题!");
        }

        return resultMap;
    }

    /**
     * 将某个提案拒绝通过审核
     * @param proposalId 提案的id
     * @return {"status" : true/false, "result" : 错误的原因}
     */
    @RequestMapping(value = "/checkFailed/{proposalId}", method = RequestMethod.POST)
    @ResponseBody
    @UseSecurity(category = "02")
    public Map<String, Object> checkFailed(@PathVariable int proposalId,
                                           @RequestBody String refuseResult,
                                           HttpSession session) {

        String userId = (String)session.getAttribute("loginUserId");
        Delegate checker = delegateService.getDelegateById(userId);
        Proposal proposal = proposalService.getProposalById(proposalId);

        Map<String, Object> resultMap = new LinkedHashMap<>();

        if (checkProposalDepartmentCheck(checker, proposal)) {
            int result = proposalService.checkFailed(proposalId, refuseResult);
            if (result == 0) {
                resultMap.put("status", true);
                messageService.sendMessageToUser(
                        proposal.getAuthor().getId(),
                        "很抱歉，您的提案《"+proposal.getTitle()+"》没有通过初审，原因是："+refuseResult, true);
            } else if (result == 1) {
                resultMap.put("status", false);
                resultMap.put("result", "访问数据库时遇到错误！");
            } else {
                resultMap.put("status", false);
                resultMap.put("result", "该提案此时不能执行该动作！");
            }
        } else {
            resultMap.put("state", false);
            resultMap.put("result", "权限问题!");
        }

        return resultMap;
    }

    /**
     * 将某个提案通过立案
     * @param reqMap 请求map
     * @param session inject
     * @return resMap
     */
    @RequestMapping(value = "/registPass", method = RequestMethod.POST)
    @ResponseBody
    @UseSecurity(category = "0700")
    public Map<String, Object> registPass(@RequestBody Map<String, Object> reqMap,
                                         HttpSession session) {
        Integer proposalId = (Integer)reqMap.get("id");
        Integer typeValue = Integer.parseInt((String) reqMap.get("type"));
        EProposalType proposalType = EProposalType.values()[typeValue];
        String hostDepartmentId = (String)reqMap.get("hostDepartmentId");
        List<String> assistDepartmentIdList = (List)reqMap.get("assistDepartmentIdList");

        Map<String, Object> resultMap = new LinkedHashMap<>();

        int result = proposalService.registPass(
                proposalId, proposalType, hostDepartmentId, assistDepartmentIdList);
        if (result == 0) {
            resultMap.put("status", true);
            Proposal proposal = proposalService.getProposalById(proposalId);
            messageService.sendMessageToUser(
                    proposal.getAuthor().getId(),
                    "恭喜！您的提案《"+proposal.getTitle()+"》已立案！", true);
        } else if (result == 1) {
            resultMap.put("status", false);
            resultMap.put("result", "访问数据库时遇到错误！");
        } else {
            resultMap.put("status", false);
            resultMap.put("result", "该提案此时不能执行该动作！");
        }

        return resultMap;
    }

    /**
     * 拒绝某个提案的立案操作
     * @param proposalId 待操作提案的id
     * @param refuseResult 拒绝的理由
     * @param session injection
     * @return json map
     */
    @ResponseBody
    @RequestMapping(value = "/registFailed/{proposalId}", method = RequestMethod.POST)
    @UseSecurity(category = "0700")
    public Map<String, Object> registFailed(@PathVariable int proposalId,
                                            @RequestBody String refuseResult,
                                            HttpSession session) {
        int result = proposalService.registFailed(proposalId, refuseResult);
        Map<String, Object> resMap = new LinkedHashMap<>();
        switch (result) {
            case 0:
                resMap.put("state", true);
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "数据库操作异常");
                break;
            case 2:
                resMap.put("state", false);
                resMap.put("result", "该提案不在可以进行该操作的状态");
                break;
            default:
                break;
        }
        return resMap;
    }

    /**
     * 部门接收某个提案
     * @param reqMap 请求map
     * @param session inject
     * @return resMap
     */
    @RequestMapping(value = "/feedbackPass", method = RequestMethod.POST)
    @ResponseBody
    @UseSecurity(category = "0800")
    public Map<String, Object> feedbackPass(@RequestBody Map<String, Object> reqMap,
                                          HttpSession session) {
        String handlerId = (String) session.getAttribute("loginUserId");
        Integer proposalId = (Integer)reqMap.get("id");
        Map<String, Object> resultMap = new LinkedHashMap<>();

        int result = proposalService.feedbackPass(proposalId, handlerId);
        if (result == 0) {
            resultMap.put("status", true);
        } else if (result == 1) {
            resultMap.put("status", false);
            resultMap.put("result", "访问数据库时遇到错误！");
        } else if (result == 2) {
            resultMap.put("status", false);
            resultMap.put("result", "该提案此时不能执行该动作！");
        } else {
            resultMap.put("status", false);
            resultMap.put("result", "操作者不是本部门成员！");
        }

        return resultMap;
    }

    /**
     * 提案的反馈信息上传
     *
     * @param proposalId 提案id
     * @param feedbackInfo 反馈信息
     * @param session injection
     *
     * @return resultMap
     */
    @RequestMapping(value = "/delegateFeedback/{proposalId}", method = RequestMethod.POST)
    @ResponseBody
    @UseSecurity(category = "0100")
    public Map<String, Object> delegateFeedback(
            @PathVariable int proposalId,
            @RequestBody String feedbackInfo,
            HttpSession session) {
        String userId = (String)session.getAttribute("loginUserId");
        Map<String, Object> resultMap = new LinkedHashMap<>();
        if (feedbackInfo == null || feedbackInfo.equals("") || feedbackInfo.length() < 10) {
            resultMap.put("state", false);
            resultMap.put("result", "反馈信息必须超过10个字!");
            return resultMap;
        }
        int result = proposalService.delegateFeedback(proposalId, userId, feedbackInfo);
        switch (result) {
            case 0:
                resultMap.put("state", true);
                break;
            case 1:
                resultMap.put("state", false);
                resultMap.put("result", "提案不在可以反馈的状态!");
                break;
            case 2:
                resultMap.put("state", false);
                resultMap.put("result", "提案只能被创建者反馈!");
                break;
            case 3:
                resultMap.put("state", false);
                resultMap.put("result", "数据库异常!");
                break;
        }

        return resultMap;
    }

    /**
     * 部门拒绝接收提案
     * @param proposalId 提案id
     * @param refuseResult 拒绝的理由
     * @param session injection
     * @return resMap
     */
    @ResponseBody
    @RequestMapping(value = "/feedbackFailed/{proposalId}", method = RequestMethod.POST)
    @UseSecurity(category = "0800")
    public Map<String, Object> feedbackFailed(@PathVariable int proposalId,
                                            @RequestBody String refuseResult,
                                            HttpSession session) {
        String handlerId = (String) session.getAttribute("loginUserId");
        int result = proposalService.feedbackFailed(proposalId, refuseResult, handlerId);
        Map<String, Object> resMap = new LinkedHashMap<>();
        switch (result) {
            case 0:
                resMap.put("state", true);
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "数据库操作异常!");
                break;
            case 2:
                resMap.put("state", false);
                resMap.put("result", "该提案不在可以进行该操作的状态!");
                break;
            case 3:
                resMap.put("state", false);
                resMap.put("result", "操作者不是本部门成员!");
                break;
            default:
                break;
        }
        return resMap;
    }

    /**
     * 获取最新提案列表
     * @return resultMap
     */
    @RequestMapping("/newProposalList/{pageSize}/{pageNo}")
    @ResponseBody
    public Map<String, Object> getNewProposalList(@PathVariable int pageSize, @PathVariable int pageNo) {
        Map<String, Object> resultMap = new LinkedHashMap<>();
        try {
            StringBuffer rowCount = new StringBuffer();
            List<Proposal> list = proposalService.getNewProposalList(pageSize, pageNo, rowCount);
            Integer pageCount = (Integer.parseInt(rowCount.toString()) - 1) / pageSize + 1;
            resultMap.put("pageCount", pageCount);
            resultMap.put("state", true);
            resultMap.put("list", list);
        } catch (Exception e) {
            resultMap.put("state", false);
            resultMap.put("result", "数据库查询发生错误");
        }
        return resultMap;
    }

    /**
     * 获取最热提案
     * @param pageSize 。。
     * @param pageNo 。。
     * @return 。。
     */
    @RequestMapping("/hotestProposalList/{pageSize}/{pageNo}")
    @ResponseBody
    public Map<String, Object> getHotestProposalList(@PathVariable int pageSize, @PathVariable int pageNo) {
        Map<String, Object> resultMap = new LinkedHashMap<>();
        try {
            StringBuffer rowCount = new StringBuffer();
            List<Proposal> list = proposalService.getHotestProposalList(pageSize, pageNo, rowCount);
            Integer pageCount = (Integer.parseInt(rowCount.toString()) - 1) / pageSize + 1;
            resultMap.put("pageCount", pageCount);
            resultMap.put("state", true);
            resultMap.put("list", list);
        } catch (Exception e) {
            resultMap.put("state", false);
            resultMap.put("result", "数据库查询发生错误");
        }
        return resultMap;
    }

    @RequestMapping(value = "/proposalsByConfigId/{configId}")
    @ResponseBody
    public Map<String, Object> getProposalsByConfigId(@PathVariable int configId,
                                                      @RequestBody Map<String, Object> reqMap) {
        Map<String, Object> resultMap = new LinkedHashMap<>();
        try {
            int pageSize = (int) reqMap.get("pageSize");
            int pageNo = (int) reqMap.get("pageNo");
            StringBuffer rowCount = new StringBuffer();
            List<Proposal> list = proposalService.getProposalsByConfigId(configId, pageSize, pageNo, rowCount);
            Integer pageCount = (Integer.parseInt(rowCount.toString()) - 1) / pageSize + 1;
            resultMap.put("pageCount", pageCount);
            resultMap.put("state", true);
            resultMap.put("list", list);
        } catch (Exception e) {
            resultMap.put("state", false);
            resultMap.put("result", "数据库查询发生错误");
        }
        return resultMap;
    }



    /*-----  页面跳转  -----*/
    @RequestMapping("/listPage")
    public String listPage() {
        return "proposal/listPage";
    }
    /**
     * 跳转到某个提案的详情显示页
     */
    @RequestMapping("/detailPage")
    public String viewProposalPage() {
        return "proposal/detailPage";
    }

    /*****************依赖注入相关↓*****************/
    @Resource
    DelegateService delegateService;
    @Resource
    ProposalService proposalService;
    @Resource
    MessageService messageService;
    @Resource
    DepartmentService departmentService;
    @Resource
    AttachmentService attachmentService;

    public DelegateService getDelegateService() {
        return delegateService;
    }

    public void setDelegateService(DelegateService delegateService) {
        this.delegateService = delegateService;
    }

    public ProposalService getProposalService() {
        return proposalService;
    }

    public void setProposalService(ProposalService proposalService) {
        this.proposalService = proposalService;
    }

    public MessageService getMessageService() {
        return messageService;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public DepartmentService getDepartmentService() {
        return departmentService;
    }

    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    public AttachmentService getAttachmentService() {
        return attachmentService;
    }

    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }
}
